package com.os.mapper.system;

import com.os.model.system.SysRole;
import com.os.model.system.SysRolePrivilege;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Author Matt
 * @Date 2022-07-08
 * @Desc:
 * @Version V1.0
 */
public interface RoleMapper {

    SysRole selectRoleById(Long id);

    List<SysRolePrivilege> selectAllRoleAndPrivileges();

    @Select({"select id, role_name roleName, enabled, create_by createBy, create_time createTime " +
            "from sys_role " +
            "where id = #{id}"})
    SysRole selectById(Long id);

    @Results(id = "roleResultMap", value = {
            @Result(property = "id", column = "id", id = true),
            @Result(property = "enabled", column = "enabled"),
            @Result(property = "roleName", column = "role_name", id = true),
            @Result(property = "createBy", column = "create_by", id = true),
            @Result(property = "createTime", column = "create_time", id = true),
    })
    @Select({"  select id, role_name, enabled, create_by, create_time " +
             "  from sys_role " +
             "  where id = #{id}"})
    SysRole selectById2(Long id);

    @ResultMap("roleResultMap")
    @Select("  select * from sys_role")
    List<SysRole> selectAll();

    //不需要返回主键
    @Insert("  insert into sys_role(id, role_name, enabled, create_by, create_time)" +
            "  values(#{id}, #{roleName}, #{enabled}, #{createBy}, #{createTime, jdbcType=TIMESTAMP})")
    int insert(SysRole sysRole);

    //需要返回主键，返回自增主键
    @Insert("  insert into sys_role(role_name, enabled, create_by, create_time)" +
            "  values(#{roleName}, #{enabled}, #{createBy}, #{createTime, jdbcType=TIMESTAMP})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert2(SysRole sysRole);

    //返回非自增主键
    @Insert("  insert into sys_role(role_name, enabled, create_by, create_time)" +
            "  values(#{roleName}, #{enabled}, #{createBy}, #{createTime, jdbcType=TIMESTAMP})")
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "id", resultType = Long.class, before = false)
    int insert3(SysRole sysRole);

    @Update({"  update sys_role set role_name = #{roleName}, enabled = #{enabled}, create_by = #{createBy}" +
            ", create_time = #{createTime, jdbcType=TIMESTAMP} where id = #{id}"})
    int updateById(SysRole sysRole);

    @Delete({"  delete from sys_role where id = #{id}"})
    int deleteById(Long id);

    List<SysRole> selectRoleByUserId(Long userId);

    List<SysRole> selectRoleByUserIdChoose(Long userId);

}
